﻿using System;
using System.Collections.Generic;
using System.Runtime.CompilerServices;

public static class DictionaryExtension
{
    public static TValue GetOrInit<Tkey, TValue>(this Dictionary<Tkey, TValue> dic, Tkey key, Func<TValue> defaultValueGetter)
    {
        if (!dic.ContainsKey(key))
        {
            dic.Add(key, defaultValueGetter());
        }
        if (dic[key] == null)
        {
            dic[key] = defaultValueGetter();
        }
        return dic[key];
    }
}

